'use strict';
const BaseController = require('./base');
const { addRules, queryRules, deleteRules } = require('../validator/comment/index');
class ThemeComment extends BaseController {

  // 新增评论
  async add() {
    const { ctx, service, app } = this;
    const errors = app.validator.validate(addRules, ctx.request.body);
    errors && ctx.helper.commonErr('参数错误', { beans: errors });
    const id = ctx.helper.decodeJwt(ctx);
    const data = ctx.request.body;
    const res = await service.themeComment.add({
      ...data,
      user_id: id,
    });
    ctx.helper.success({ bean: res }, '新增评论成功');
  }

  // 查询
  async query() {
    const { ctx, service, app } = this;
    const errors = app.validator.validate(queryRules, ctx.request.query);
    errors && ctx.helper.commonErr('参数错误', { beans: errors });
    const user_id = ctx.helper.decodeJwt(ctx);
    const themeId = ctx.params.id;
    const data = ctx.request.query;
    const res = await service.themeComment.query(themeId, user_id, data);
    ctx.helper.success({ beans: res }, '查询评论成功');
  }

  // 编辑
  async delete() {
    const { ctx, service, app } = this;
    const errors = app.validator.validate(deleteRules, ctx.request.body);
    errors && ctx.helper.commonErr('参数错误', { beans: errors });
    const { id } = ctx.request.body;
    const res = await service.themeComment.delete(id, ctx.helper.time());
    ctx.helper.success({ bean: res }, '删除评论成功');
  }
}

module.exports = ThemeComment;
